package db

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

func InitDB() (err error) {
	db, err = gorm.Open("mysql", "root:ZAQ!@WSX@tcp(localhost:3306)/sola?charset=utf8&parseTime=True&loc=Local")
	//db, err = gorm.Open("mysql", "sola:sola@tcp(localhost:3306)/sola?charset=utf8&parseTime=True&loc=Local")
	db = db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8 auto_increment=1")
	if err != nil {
		fmt.Println("Init DB Error:", err)
		return
	}
	autoMigrateModels()
	return
}

func ClosDB() error {
	if db != nil {
		return db.Close()
	}
	return nil
}

func autoMigrateModels() {
	db.AutoMigrate(&User{}, &Article{}, &Classify{}, &Category{}, &Tag{})
}
